package com.question.controller;

import com.question.Dto.Content;
import com.question.Dto.QuestionDto;
import com.question.Vo.QuestionImportVo;
import com.question.common.PageResult;
import com.question.common.Result;
import com.question.service.QuestionService;
import com.question.utils.ExcelUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService questionService;

    @RequestMapping("/template")
    public ResponseEntity<byte[]> downloadTemplate() throws IOException {
        //获取模板下字节数组
        byte[] template = ExcelUtil.generateTemplate();
        //封装ResponseEntity
        ResponseEntity<byte[]> responseEntity = ResponseEntity.ok().header("Content-Disposition", "attachment;filename=question_import_template.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM).body(template);
        return responseEntity;
    }

    @RequestMapping("/import")
    public Result imports(MultipartFile file) throws IOException {
        List<QuestionImportVo> list = ExcelUtil.parseExcek(file);
        questionService.imports(list);


        return Result.success();
    }

    @RequestMapping("/list")
    public Result list(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = new PageResult();
        pageResult = questionService.selectByPage(pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody QuestionDto questionDto) {
        System.out.println(questionDto.getName());
        questionService.addquestion(questionDto);
        return Result.success();

    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody QuestionDto questionDto){
        questionService.editQuestion(questionDto);
        return Result.success();

    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        questionService.deleteById(id);
        return Result.success();
    }
}
